package com.fosun.spr.plugin.learn.api.v2User.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fosun.spr.plugin.learn.api.v2User.dao.V2UserDao;
import com.fosun.spr.plugin.learn.api.v2User.service.V2UserService;
import com.fosun.spr.plugin.learn.api.v2User.service.model.CreateOrUpdateSpModel;
import com.fosun.spr.plugin.learn.api.v2User.service.model.CreateOrUpdateSrModel;
import com.fosun.spr.plugin.learn.api.v2User.service.model.DeleteSpModel;
import com.fosun.spr.plugin.learn.api.v2User.service.model.DeleteSrModel;
import com.fosun.spr.plugin.learn.api.v2User.service.model.GetSpModel;
import com.fosun.spr.plugin.learn.api.v2User.service.model.GetSrModel;
import com.fosun.spr.plugin.learn.api.v2User.service.model.ListSpModel;
import com.fosun.spr.plugin.learn.api.v2User.service.model.ListSrModel;
import com.fosun.spr.plugin.learn.helper.mybatis.dao.V2UserMapper;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2User;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2User;
import com.fosun.spr.plugin.learn.helper.mybatis.model.V2UserExample;
import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;
import com.fosun.spr.core.utils.ExtAsserts;


@Transactional(rollbackFor = { RuntimeException.class, Exception.class })
@Service
public class V2UserServiceImpl extends CoBaseService implements
		V2UserService {
	@Autowired 
	private V2UserDao v2UserDao;
	@Autowired
	private V2UserMapper v2UserMapper;

	@Override
	public ListSrModel list(ListSpModel spModel) {
		ListSrModel srModel = new ListSrModel();
		srModel.setDatas(v2UserDao.list(spModel));
		srModel.setTotalCount(v2UserDao.listCount(spModel));
		return srModel;
	}

	@Override
	public GetSrModel get(GetSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		GetSrModel srModel = new GetSrModel();
		V2User db = v2UserMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db,"用户管理-v2_user");
		cglibCopyProperties(db, srModel, true);	
		return srModel;
	}
	
	@Override
	public DeleteSrModel delete(DeleteSpModel spModel) {
		ExtAsserts.notNull(spModel.getId(),"id");
		DeleteSrModel srModel = new DeleteSrModel();
		V2User db = v2UserMapper.selectByPrimaryKey(spModel.getId());
		ExtAsserts.notNull(db);
		db.setIsDeleted(IS_DELETED_Y);
		db.setRecUpdateTime(new Date());
		v2UserMapper.updateByPrimaryKey(db);
		return srModel;
	}
	
	@Override
	public CreateOrUpdateSrModel createOrUpdate(CreateOrUpdateSpModel spModel) {
		ExtAsserts.notNull(spModel.getUserName(),"用户名");
		ExtAsserts.notNull(spModel.getFilePortraitImage(),"头像链接地址");
		
		CreateOrUpdateSrModel srModel = new CreateOrUpdateSrModel();
		//唯一性校验
		V2UserExample eUserName = new V2UserExample();
		V2UserExample.Criteria cUserName = eUserName.createCriteria();
		cUserName.andUserNameEqualTo(spModel.getUserName());
		cUserName.andIsDeletedEqualTo(IS_DELETED_N);
		List<V2User> rowsUserName = v2UserMapper.selectByExample(eUserName);		
		
		//if(StringUtils.isBlank(spModel.getId())){
		if(spModel.getId()==null){
			if(rowsUserName.size()>0) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getUserName()+" 已经存在");
			}		
			
			V2User db = new V2User();
			cglibCopyProperties(spModel, db, true);
			db.setIsDeleted(IS_DELETED_N);
			db.setRecCreateTime(new Date());
			//db.setId(getUuid());
			v2UserMapper.insert(db);
			srModel.setId(db.getId());
		}else{
			if(rowsUserName.size()>1) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getUserName()+" 已经存在");
			}			
			if(rowsUserName.size()==1 && !rowsUserName.get(0).getUserName().equals(spModel.getUserName())) {
				throw new ServiceException(ServiceException.CODE_ERR,spModel.getUserName()+" 已经存在");
			}	
			
			V2User db = v2UserMapper.selectByPrimaryKey(spModel.getId());
			db.setUserName(spModel.getUserName());
			db.setFilePortraitImage(spModel.getFilePortraitImage());
			v2UserMapper.updateByPrimaryKey(db);
		}
		return srModel;
	}	
}
